CREATE PROCEDURE [sp_ssiscatalog].[exec]
	@exec_id									BIGINT						
,	@exec_execution								BIT					=	1	
,	@exec_events								BIT					=	1	
,	@exec_errors								BIT					=	1	
,	@exec_warnings								BIT					=	1	
,	@exec_executable_stats						BIT					=	1	
,	@exec_events_packagesexcluded				NVARCHAR(MAX)		=	NULL
,	@show_docs									BIT					=	1		
,	@show_docs_only								BIT					=	0
AS
BEGIN
		SET NOCOUNT ON;
		DECLARE	@vDocs	NVARCHAR(MAX)	=	N'Parameters:
	@exec_id									BIGINT											/*Specify which execution we want to return information for*/
,	@exec_execution								BIT					=	1						/*Return a one-row result set showing information about the execution*/
,	@exec_events								BIT					=	1						/*Return a result set showing all events*/
,	@exec_errors								BIT					=	1						/*Return a result set showing all errors*/
,	@exec_warnings								BIT					=	1						/*Return a result set showing all warnings*/
,	@exec_executable_stats						BIT					=	1						/*Return a result set showing all executable stats*/
,	@exec_events_packagesexcluded				NVARCHAR(MAX)		=	NULL					/*Specify a list of packages for which events should not be displayed*/
';
		EXEC	[sp_ssiscatalog].[show_docs]
					@docs			=	@vDocs
				,	@action			=	N'exec'
				,	@show_docs		=	@show_docs;
		IF (@show_docs_only = 1)
				RETURN;
		BEGIN TRY
				IF (@exec_id IS NULL)
				BEGIN
						SELECT	@exec_id = MAX(e.[execution_id])
						FROM	[$(SSISDB)].[catalog].[executions]e;
						RAISERROR('No execution_id specified. Defaulting to most recent execution (execution_id=%d)',1,1,@exec_id);
				END              
				IF NOT EXISTS (SELECT 1 FROM [$(SSISDB)].[catalog].[executions] WHERE [execution_id] = @exec_id)
						RAISERROR('%d is not a known execution id or you do not have permission to access information about that execution',1,1,@exec_id); --Note low severity.
				ELSE
				BEGIN
						IF (@exec_execution = 1)
								SELECT	e.[ResultSet],e.[folder_name],e.[project_name],e.[package_name],e.[executed_as_name],e.[status_desc],e.[start_time],e.[end_time],e.duration_in_seconds,e.[execution_id]
								FROM	[sp_ssiscatalog].[executions](@exec_id,DEFAULT,DEFAULT,DEFAULT,DEFAULT,DEFAULT,DEFAULT,DEFAULT) e;
								RAISERROR('%d executions returned',1,1,@@ROWCOUNT);

						IF (@exec_events = 1)
								EXEC	[sp_ssiscatalog].[get_events]				@exec_id									=	@exec_id
																	,	@exec_events_packagesexcluded				=	@exec_events_packagesexcluded;

								
						IF(@exec_errors = 1)
								EXEC	[sp_ssiscatalog].[get_filteredevents]		@exec_id	=	@exec_id
																	,	@events		=	N'OnError,OnTaskFailed';

						IF(@exec_warnings = 1)
								EXEC	[sp_ssiscatalog].[get_filteredevents]		@exec_id	=	@exec_id
																	,	@events		=	N'OnWarning';
						IF(@exec_executable_stats = 1)
								EXEC	[sp_ssiscatalog].[get_executable_stats]	@exec_id	=	@exec_id;
				END              
		END TRY
		BEGIN CATCH      
				THROW;
		END CATCH
END
	